﻿#region License

// Droid Manager - An Android Phone Tools Suite
// Copyright (C) 2011
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;
using DroidManager.Log;
using DroidManager.Plugins.API;
using DroidManager.Plugins.Interfaces;
using DroidManager.Data.Utils;
using System.Drawing;
using Managed.Adb;
using System.Windows.Forms;
using System.IO;

namespace ApkInstaller
{
    public class ApkInstaller : IPlugin
    {
        private Logger _log = Api.Logger;

        private Installer installerForm = null;

        public Image Icon
        {
            get { return Properties.Resources.icondroidmanager; }
        }

        public string Name
        {
            get { return "Apk Installer"; }
        }

        public bool ShowInMenu
        {
            get { return true; }
        }

        private string AlternateInstall()
        {
            IniFile settings = Api.SettingsFile(this);
            if (File.Exists(Api.SettingsFilePath(this)))
            {
                return settings.IniReadValue("Settings", "alternate_install");
            }
            else
            {
                settings.IniWriteValue("Settings", "alternate_install", "0");
                return "0";
            }
        }

        public void OnStart(Device device, string[] parameters)
        {
            try
            {
                if (device != null)
                {
                    if (parameters != null && parameters.Length > 0)
                    {
                        if (System.IO.File.Exists(parameters[0]))
                        {
                            Application.Run(installerForm = new Installer(device, parameters[0], AlternateInstall()));
                        }
                        else
                        {
                            MessageBox.Show(parameters[0] + " does not exist!", "Droid Manager - Apk Installer",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                            return;
                        }
                    }
                    else
                    {
                        OpenFileDialog openApk = new OpenFileDialog();
                        openApk.Filter = "Apk Files|*.apk";
                        openApk.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
                        openApk.Multiselect = false;
                        if (openApk.ShowDialog() == DialogResult.OK)
                        {
                            Application.Run(installerForm = new Installer(device, openApk.FileName, AlternateInstall()));
                        }
                    }
                }
                else
                {
                    MessageBox.Show("You must specify a device!", "Droid Manager - Apk Installer", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    return;
                }
            }
            catch (ObjectDisposedException)
            {
                return;
            }

            catch (Exception e)
            {
                _log.Error("Apk Installer: " + e.ToString());
                MessageBox.Show(e.ToString(), "Error | Apk Installer - Droid Manager", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                if (installerForm != null)
                {
                    installerForm.Close();
                }
                return;
            }
        }
    }
}